<template>
  <div id="app"
       v-bar>
    <div id="scroll-container">
      <router-view />
    </div>
  </div>
</template>

<script>
import withOutLoginPath from '@/utils/withOutLoginPath'
import zh_CN from "ant-design-vue/lib/locale-provider/zh_CN";
import moment from "moment";
import "moment/locale/zh-cn";
moment.locale("zh-cn");
export default {
  data () {
    return {
      hasRefreshUserRights: false,
      zh_CN,
    }
  },
  created () {
    this.initUserRights();
  },
  methods: {
    // 不需要登录的路由页面
    withOutLogin () {
      let flag = true;
      let path = this.$route.path, meta = this.$route.meta;
      if (meta.unLogin) {
        flag = false;
      } else {
        for (let item of withOutLoginPath) {
          if (path.indexOf(item) === 0) {
            flag = false;
          }
        }
      }
      console.log(path, flag, '是否退出登录')
      if (flag) {
        this.$router.push('/')
      }
    },
    // 初始化用户权限
    initUserRights () {
      if (this.currentTenant) {
        if (this.hasRefreshUserRights) return;
        this.hasRefreshUserRights = true;
        this.$store.dispatch('unSave/switchCurrent').then(() => {
          this.closeHasRefreshUserRights();
        }).catch(() => {
          this.closeHasRefreshUserRights();
        })
      } else {
        this.$store.commit('unSave/removeUserRights');
      }
    },
    // 关闭已经刷新用户权限的判断
    closeHasRefreshUserRights () {
      let timer = setTimeout(() => {
        this.hasRefreshUserRights = false;
        clearTimeout(timer);
      }, 2000);
    }
  },
  computed: {
    tokenValue () {
      return this.$store.state.user.tokenValue
    },
    currentTenant () {
      return this.$store.state.user.currentTenant
    }
  },
  watch: {
    tokenValue (newVal) {
      if (!newVal) {
        this.withOutLogin();
      }
    },
    // 如果当前的登录人的企业发生变化，获取对应的权限
    currentTenant (newVal) {
      this.initUserRights(newVal);
    },
    '$route.path' () {
      if (!this.tokenValue) {
        this.withOutLogin();
      }
    }
  }
}
</script>

<style lang="less">
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif, PingFangSC,
    PingFangSC-Regular;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  color: #2c3e50;
}
</style>
